#encoding:utf-8
from flask import render_template, redirect, request, url_for, flash
from flask_login import login_user, logout_user, login_required, \
    current_user
from . import admin
from .. import db
from ..models import User,Permission
from ..email import send_email
from .forms import LoginForm, RegistrationForm, \
    PasswordResetRequestForm, ChangePasswordForm,ResetForm
from ..decorators import permission_required


# @auth.before_app_request
# def before_request():
#     if current_user.is_authenticated:
#         current_user.ping()
        # if not current_user.confirmed \
        #         and request.endpoint \
        #         and request.blueprint != 'auth' \
        #         and request.endpoint != 'static':
        #     return redirect(url_for('auth.unconfirmed'))


# @auth.route('/unconfirmed')
# def unconfirmed():
#     if current_user.is_anonymous or current_user.confirmed:
#         return redirect(url_for('main.index'))
#     return render_template('auth/unconfirmed.html')


# @auth.route('/login', methods=['GET', 'POST'])
# def login():
#     form = LoginForm()
#     flash('')
#     if form.validate_on_submit():
#         user = User.query.filter_by(email=form.email.data).first()
#         if user is not None and user.verify_password(form.password.data):
#             login_user(user, form.remember_me.data)
#             next = request.args.get('next')
#             if next is None or not next.startswith('/'):
#                 next = url_for('main.index')
#             return redirect(next)
#         flash('用户名或密码错误！')
#     return render_template('auth/login.html', form=form)

#用户信息
@admin.route('/usermassage')
@login_required
@permission_required(Permission.ADMINALL)
def userMassage():
    users = User.query.all()
    return render_template('admin/userMassage.html',users = users)

#升级为管理员
@admin.route('/upgrade/<id>', methods=['GET', 'POST'])
@login_required
@permission_required(Permission.ADMINALL)
def upgrade(id):
    user = User.query.filter_by(id=id).first()
    if user.role_id == 1:
        user.role_id = 2
    db.session.add(user)
    db.session.commit()
    return redirect('admin/usermassage')

#降级为用户
@admin.route('/downgrade/<id>', methods=['GET', 'POST'])
@login_required
@permission_required(Permission.ADMINALL)
def downgrade(id):
    user = User.query.filter_by(id=id).first()
    if user.role_id == 2:
        user.role_id = 1
    db.session.add(user)
    db.session.commit()
    return redirect('admin/usermassage')

#删除用户
@admin.route('/delete/<id>', methods=['GET', 'POST'])
@login_required
@permission_required(Permission.ADMINALL)
def delete(id):
    user = User.query.filter_by(id=id).first()
    if user.role_id <= 2:
        db.session.delete(user)
        db.session.commit()
        return redirect('admin/usermassage')
    flash('无法删除超级管理员！')
    return redirect('admin/usermassage')
# @admin.route('/')
# @login_required
# @permission_required(Permission.ADMINALL)
# def userMassage():
#     users = User.query.all()
#     return render_template('admin/userMassage.html',users = users)
# @auth.route('/confirm')
# @login_required
# def resend_confirmation():
#     token = current_user.generate_confirmation_token()
#     send_email(current_user.email, 'Confirm Your Account',
#                'auth/email/confirm', user=current_user, token=token)
#     flash('A new confirmation email has been sent to you by email.')
#     return redirect(url_for('main.index'))


# @auth.route('/reset', methods=['GET', 'POST'])
# def reset():
#     # if not current_user.is_anonymous:
#     #     return redirect(url_for('main.index'))
#     user = User.query.filter_by(email=current_user.email).first()
#     form = ResetForm(username=user.username,email=user.email,sex=user.sex,birthday=user.birthday)
#     if form.validate_on_submit():
#         user.email = form.email.data
#         user.username = form.username.data
#         user.sex = form.sex.data
#         user.birthday = form.birthday.data
#         db.session.add(user)
#         db.session.commit()
#         flash('信息修改成功！')
#         return redirect(url_for('auth.info'))
#     return render_template('auth/reset.html',form=form)


# @auth.route('/reset/<token>', methods=['GET', 'POST'])
# def password_reset(token):
#     if not current_user.is_anonymous:
#         return redirect(url_for('main.index'))
#     form = ChangePasswordForm()
#     if form.validate_on_submit():
#         if User.reset_password(token, form.password.data):
#             db.session.commit()
#             flash('Your password has been updated.')
#             return redirect(url_for('auth.login'))
#         else:
#             return redirect(url_for('main.index'))
#     return render_template('auth/resetpassword.html', form=form)
